import { CONFIG } from "../../config";
const WebSocket = require('ws');
const schedule = require('node-schedule');


export class WSService {
    private wss: any;
    constructor(server) {
        this.wss = new WebSocket.Server({ server });
        this.wss.on('connection',(ws)=>this.connection.call(this,ws));
    }

   /**
    * 连接成功
    * @param ws 
    */
    public connection(ws)
    {
        console.log('创建服务端链接成功！');
        this.schedule.call(this); 
        ws.on('message', (data)=>this.receiveMessage.call(this,data));
    }


    public receiveMessage(data)
    {
        console.log("收到消息");
        console.log(data); 
    }




 
   /**
    * 转发消息给所有连接客户端
    * @param data 
    */
    public snedMessage(data)
    {
        this.wss.clients.forEach(function each(client) {
            client.send(data);
        });
    }



   public schedule()
   { 
      setInterval(()=>function(){
 
      },3600);
   }



    // public loadNode() {
    //     let $this = this;
    //     CONFIG.wsArray.forEach((ws) => {
    //         $this.connectedNode(ws);
    //     });
    // }

 


}


